<?php
use Aws\S3\Enum\Status;
defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends Auth_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('order_mdl');
    }

    public function index(){

        //组装查询
        $this->db->start_cache();
        $start = $this->input->get('start',TRUE);
        $start = strtotime($start);
        if($start){
            $this->db->where('create_time >=' , $start);
        }

        $end = $this->input->get('end',TRUE);
        $end = strtotime($end);
        if($end){
            $this->db->where('create_time <=' , $end);
        }

        //根据订单号查询
        $oid = $this->input->get('oid',TRUE);
        $keywords = $this->input->get('keywords',TRUE);

        if(!empty($oid) && !empty($keywords)){
            if($oid == 'oid'){
                $this->db->where('order_id',intval($keywords));
            }
            //收信人名称
            if($oid == 'c_name'){
                $this->db->stop_cache();
                //查询当前收信人名称的数据
                $this->db->start_cache();
            }
        }
        
        //根据订单状态
        $status = $this->input->get('last',TRUE);
        if(!empty($status)){
        	
        	$status = $status == 9 ? '0' : $status;
        	$this->db->where('status',$status);
        }

        $this->db->stop_cache();

        //查询订单
        $this->load->library('pagination');
        $config['base_url'] = site_url('admini/order/index');
        $config['total_rows'] = $this->order_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        //分页
        $this->pagination->initialize($config);
        $page = $this->pagination->create_links();

        //查询数据
        $per_page = $this->input->get('per_page',TRUE);
        $this->db->limit($config['per_page'],intval($per_page));
        
        $list = $this->order_mdl->select();
        $this->db->flush_cache();
        $list = $this->order_mdl->restr($list);

        $this->db->flush_cache();

        $data = array(
            'page'=>$page,
            'list'=>$list
            );

        $this->_view('admin/order_index',$data);
        
    }

    /**
     * 订单详细信息
     */
    public function info(){
        $oid = $this->input->get('oid',TRUE);
        $oid = intval($oid);
        if(empty($oid)){
            show_404();
        }
        
        $this->db->where('id',$oid);
        $res = $this->order_mdl->get_one();
        print_r($res);
        //查询付款方式
        $this->load->model('payin_mdl');
        $this->db->where('id',$res['payin_id']);
        $res_pay = $this->payin_mdl->get_one();
        
        //查询收货人信息
        $this->load->model('cust_mdl');
        $this->db->where('id',$res['cust_id']);
        $res_cust = $this->cust_mdl->get_one();
        
        //查询该订单的所有商品
        $this->load->model('goods_order_mdl');
        $list_goods = $this->goods_order_mdl->get_goods($oid);
        
        //print_r($list_goods);
        if(empty($res)){
        	show_error('参数错误！');
        }
        
        $data = array(
            'res' => $res,
        	'payin' => $res_pay['title'],
        	'cust' => $res_cust,
        	'goods' => $list_goods
        );


        $this->_view('admin/order_info',$data);
    }
    
    /**
     * 设置用户订单状态
     */
    public function set_status(){
    	if(!$this->input->is_ajax_request()){
    		show_404();
    	}
    	
    	$post = $this->input->post(NULL,TRUE);
    	
    	
    	if(empty($post)){
    		die('参数错误');
    	}
    	
    	$oid = intval($post['oid']);
    	$status = intval($post['status']);
    	
    	if(empty($oid) || empty($status)){
    		die('参数错误');
    	}
    	
    	$this->db->where('id',$oid);
    	$res = $this->order_mdl->update(array('status' => "$status",'endtime'=>time()));
    	//echo $this->db->last_query();
    	
    	echo $res === true ? 1 : '修改订单失败！';
    }

}

/* End of file order.php */
/* Location: ./application/controllers/admini/order.php */